package com.quicksure.mobile.serviceImpl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.quicksure.mobile.common.ProcessData;
import com.quicksure.mobile.constants.LudiConstants;
import com.quicksure.mobile.dms.PremiumDataManageService;
import com.quicksure.mobile.dms.VHLDataManageService;
import com.quicksure.mobile.entity.Baseinfor;
import com.quicksure.mobile.entity.Coverageinfor;
import com.quicksure.mobile.entity.InsuranceDetailsVO;
import com.quicksure.mobile.entity.Vhlinfor;
import com.quicksure.mobile.service.PremiumCountService;
import com.quicksure.mobile.sinosafefactory.Sinosafeinterface;
import com.quicksure.mobile.utility.StringUtils;

@Service("premiumCountService")
public class PremiumCountServiceImpl implements PremiumCountService {

	private static final Logger logger = Logger.getLogger(PremiumCountServiceImpl.class);
	@Resource
	private Sinosafeinterface sinosafeinterface;
	@Resource 
	private VHLDataManageService dataManageService;
	@Resource
	private PremiumDataManageService premiumDataManageService;

	
	
	/**
	 * 试算的业务
	 */
	
	public InsuranceDetailsVO PremiumCount(Vhlinfor vhlinfor,Baseinfor baseinfor,List<Coverageinfor> coverageinfors,HttpServletRequest httprequest) {
		String orderNo=httprequest.getParameter("orderNo");
		HttpSession session=httprequest.getSession();
		String sypolicystartdate =null;//商业起保时间
		String sypolicyendDate="";//商业到期时间
		String jqpolicystartdate =null;//交强起保时间
		String jqpolicyendDate="";//交强到期时间
		String drvowner="";//车主姓名
		InsuranceDetailsVO insuranceDetails=null;
		//玻璃类型
		 String glassType = "";
		 //给予各个险种配置险种常量代码
		 if(coverageinfors.size()>0){
			 for(int i=0;coverageinfors.size()>i;i++){
				 if(coverageinfors.get(i).getInsrnccode()!=null){
				 if(coverageinfors.get(i).getInsrnccode().equals("102")){
					 coverageinfors.get(i).setInsrnccode(LudiConstants.THREEPARTYINSUEANCE);
				 }else if (coverageinfors.get(i).getInsrnccode().equals("101")){
					 coverageinfors.get(i).setInsrnccode(LudiConstants.LOSSINSUEANCE);
				 }else if(coverageinfors.get(i).getInsrnccode().equals("357")){
					 coverageinfors.get(i).setInsrnccode(LudiConstants.COMPULSORYINSURANCE);
				 }else if(coverageinfors.get(i).getInsrnccode().equals("104")){
					 coverageinfors.get(i).setInsrnccode(LudiConstants.DRIVERINSUEANCE);
				 }else if(coverageinfors.get(i).getInsrnccode().equals("105")){
					 coverageinfors.get(i).setInsrnccode(LudiConstants.PASSENGERINSUEANCE);
				 }else if(coverageinfors.get(i).getInsrnccode().equals("103")){
					 coverageinfors.get(i).setInsrnccode(LudiConstants.ROBBERYINSUEANCE);
				 }else if(coverageinfors.get(i).getInsrnccode().equals("108")){
					 coverageinfors.get(i).setInsrnccode(LudiConstants.SPONTANEOUSCOMBUSTIONINSUEANCE);
				 }else if(coverageinfors.get(i).getInsrnccode().equals("107")){
					 glassType=coverageinfors.get(i).getSuminsured();
					 coverageinfors.get(i).setSuminsured("1");//设置保额为1
					 coverageinfors.get(i).setBulletGlass("0");//设置默认值不是防爆玻璃
					 coverageinfors.get(i).setInsrnccode(LudiConstants.GLASSINSUEANCE);
				 }else if(coverageinfors.get(i).getInsrnccode().equals("115")){
					 coverageinfors.get(i).setInsrnccode(LudiConstants.NOTFOUNDINSUEANCE);
				 }else if(coverageinfors.get(i).getInsrnccode().equals("116")){
					 coverageinfors.get(i).setInsrnccode(LudiConstants.APPOINTINSUEANCE); 
				 }
			  }else{
				sypolicystartdate=coverageinfors.get(i).getSypolicystartdate();
				jqpolicystartdate=coverageinfors.get(i).getJqpolicystartdate();
				orderNo = 	coverageinfors.get(i).getBaseinfororderno();	 
			  }
			  
			 }		 
		 }
		if(StringUtils.checkStringEmpty(orderNo)
				&& session.getAttribute(orderNo + "insuranceDetailsVO") != null){
			 insuranceDetails = (InsuranceDetailsVO)session.getAttribute(orderNo+"insuranceDetailsVO");
			 if(insuranceDetails.getBaseinfor()!=null){
			 insuranceDetails.getBaseinfor().setJqpremium(null);
			 insuranceDetails.getBaseinfor().setSypolicyenddate(null);
			 insuranceDetails.getBaseinfor().setJqpolicyenddate(null);
			 insuranceDetails.getBaseinfor().setSypremium(null);
			 insuranceDetails.getBaseinfor().setJqpolicystartdate(null);
			 }			 
			 if(insuranceDetails.getTaxinfor()!=null){
				 insuranceDetails.getTaxinfor().setSumuptax(null);
				 insuranceDetails.getTaxinfor().setCurrenttax(null);
			 }
			 if(insuranceDetails.getVhlinfor()!=null){
				 if(insuranceDetails.getVhlinfor().getChgownerflag()!=null){
					 if(insuranceDetails.getVhlinfor().getChgownerflag().equals("0")){
						 insuranceDetails.getVhlinfor().setTransferdate(null);
					 }
				 }
				 
			 }
			}
		if (insuranceDetails == null) {			
			InsuranceDetailsVO insuranceDetailsVo=ProcessData.initInsuranceDetailsVO();
			insuranceDetails=insuranceDetailsVo;
		}

		 
		 //给予起保时间加入时分秒,后续前端控件有就删除     给予请求报文的时间都为T+1默认时间
		    Date date = new Date();
		    Calendar calendar = Calendar.getInstance();
	        calendar.setTime(date);
	        calendar.add(Calendar.DAY_OF_MONTH, +1);//+1今天的时间加一天
	        date = calendar.getTime();
	        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	        String dateString = formatter.format(date);
		 if(!"".equals(sypolicystartdate) && sypolicystartdate!=null && sypolicystartdate.length()<12 ){
			 String lcn = insuranceDetails.getVhlinfor().getLcnno();
			    if("*-*".equals(lcn)){
			    	sypolicystartdate=sypolicystartdate+" 00:00:00";
					 insuranceDetails.getBaseinfor().setSypolicystartdate(sypolicystartdate);
			    }else{
			    	sypolicystartdate=sypolicystartdate+" 00:00:00";
					 insuranceDetails.getBaseinfor().setSypolicystartdate(sypolicystartdate);
			    	//sypolicystartdate=dateString+" 00:00:00";
					//insuranceDetails.getBaseinfor().setSypolicystartdate(sypolicystartdate);   //默认去当前时间+1天的话，提前续保的单出不了。 孙小东
			    }
			 
		 }
		 if(!"".equals(jqpolicystartdate) && jqpolicystartdate!=null && jqpolicystartdate.length()<12){
			 String lcn = insuranceDetails.getVhlinfor().getLcnno();
			    if("*-*".equals(lcn)){
			    	jqpolicystartdate=jqpolicystartdate+" 00:00:00";
					 insuranceDetails.getBaseinfor().setJqpolicystartdate(jqpolicystartdate);
			    }else{
			    	jqpolicystartdate=jqpolicystartdate+" 00:00:00";
					 insuranceDetails.getBaseinfor().setJqpolicystartdate(jqpolicystartdate);
			    	//jqpolicystartdate=dateString+" 00:00:00";
			    	// insuranceDetails.getBaseinfor().setJqpolicystartdate(jqpolicystartdate);//默认去当前时间+1天的话，核心有些单子会报已经投保记录  李长立  --核心问题，年后处理
			   }
			 
		 }
		 if(insuranceDetails.getBaseinfor()!=null&&insuranceDetails.getBaseinfor().getSypolicystartdate()!=null&&insuranceDetails.getBaseinfor().getSypolicystartdate().length()>19){
        	 insuranceDetails.getBaseinfor().setSypolicystartdate(insuranceDetails.getBaseinfor().getSypolicystartdate().substring(0, insuranceDetails.getBaseinfor().getSypolicystartdate().length()-2)); 
         }
			 if(insuranceDetails.getVhlinfor()!=null){
				 if(insuranceDetails.getVhlinfor().getBrandcode()!=null){
					 insuranceDetails.getVhlinfor().setGlasstype(glassType);
				 }else{
					 insuranceDetails.setVhlinfor(vhlinfor);
					 insuranceDetails.getVhlinfor().setGlasstype(glassType);
				 }
			 }
		 
		 //第一个元素的起保日期是否为空，不为空就移除
		 if(coverageinfors.get(0).getSypolicystartdate()!=null || "".equals(coverageinfors.get(0).getSypolicystartdate())){
			 coverageinfors.remove(0);	 
		 }
		 if(baseinfor.getJqpolicystartdate()!=null || baseinfor.getJqpolicystartdate()!=""){
			 baseinfor.setJqpolicystartdate("");
			 baseinfor.setJqpolicyenddate("");
		 }
		 if(baseinfor.getSypolicystartdate()!=null || baseinfor.getSypolicystartdate()!=""){
			 baseinfor.setSypolicystartdate("");
			 baseinfor.setSypolicyenddate("");
		 }
		 insuranceDetails.setCoverageinfors(coverageinfors);
		 insuranceDetails=ProcessData.setObjValuetoinsuranceDetails(baseinfor,insuranceDetails);
         insuranceDetails.getUserinfor().setUserAction("PremiumCount");
         
         if(insuranceDetails.getVhlinfor().getDrvowner()!=null){
        	 logger.info("试算获取前台的数据车主名称为："+insuranceDetails.getVhlinfor().getDrvowner());
        	 drvowner = insuranceDetails.getVhlinfor().getDrvowner();
        	 insuranceDetails.getBaseinfor().setOwnersname(drvowner);
        	 //把车主姓名更新进base表
        	 premiumDataManageService.updatePremium(insuranceDetails);
         }
         
        
        //封装数据调用华安接口
        sinosafeinterface.performSinosafeOprations(insuranceDetails);
        //费用保留两位小数,不足不0
        DecimalFormat decimalFormat=new DecimalFormat(".00");
        for(Coverageinfor cover:insuranceDetails.getCoverageinfors()){
        	if(cover.getSuminsured()!=null&&!cover.getSuminsured().equals("")&&!cover.getSuminsured().equals("0")
        	   &&!cover.getSuminsured().equals("0.00")&&!cover.getSuminsured().equals(LudiConstants.THREE_COVERAGE_5W)
        	   &&!cover.getSuminsured().equals(LudiConstants.THREE_COVERAGE_10W)&&!cover.getSuminsured().equals(LudiConstants.THREE_COVERAGE_20W)
        	   &&!cover.getSuminsured().equals(LudiConstants.THREE_COVERAGE_30W)&&!cover.getSuminsured().equals(LudiConstants.THREE_COVERAGE_50W)
        	   &&!cover.getSuminsured().equals(LudiConstants.THREE_COVERAGE_100W)){
        		cover.setSuminsured(decimalFormat.format(Float.parseFloat(cover.getSuminsured())));
        	}
        	if(cover.getPremium()!=null&&!cover.getPremium().equals("")&&!cover.getPremium().equals("0")&&!cover.getPremium().equals("0.00")){
        		cover.setPremium(decimalFormat.format(Float.parseFloat(cover.getPremium())));
        	}
        	if(cover.getBasepremium()!=null&&!cover.getBasepremium().equals("")&&!cover.getBasepremium().equals("0")&&!cover.getBasepremium().equals("0.00")){
        		cover.setBasepremium(decimalFormat.format(Float.parseFloat(cover.getBasepremium())));
        	}
        	if(cover.getBeforepremium()!=null&&!cover.getBeforepremium().equals("")&&!cover.getBeforepremium().equals("0")&&!cover.getBeforepremium().equals("0.00")){
        		cover.setBeforepremium(decimalFormat.format(Float.parseFloat(cover.getBeforepremium())));
        	}
        	if(cover.getCyl15()!=null&&!cover.getCyl15().equals("")&&!cover.getCyl15().equals("0")&&!cover.getCyl15().equals("0.00")){
        		cover.setCyl15(decimalFormat.format(Float.parseFloat(cover.getCyl15())));
        	}
        	if(cover.getNyl12()!=null&&!cover.getNyl12().equals("")&&!cover.getNyl12().equals("0")&&!cover.getNyl12().equals("0.00")){
        		cover.setNyl12(decimalFormat.format(Float.parseFloat(cover.getNyl12())));
        	}
        	if(cover.getRiskpremium()!=null&&!cover.getRiskpremium().equals("")&&!cover.getRiskpremium().equals("0")&&!cover.getRiskpremium().equals("0.00")){
        		cover.setRiskpremium(decimalFormat.format(Float.parseFloat(cover.getRiskpremium())));
        	}
        }
        if(insuranceDetails.getBaseinfor()!=null){
        	Baseinfor bb=insuranceDetails.getBaseinfor();
        	if(bb.getTotalPremium()!=null&&!bb.getTotalPremium().equals("")&&!bb.getTotalPremium().equals("0")&&!bb.getTotalPremium().equals("0.00")){
        		bb.setTotalPremium(decimalFormat.format(Float.parseFloat(bb.getTotalPremium())));
        	}
        	if(bb.getJqpremium()!=null&&!bb.getJqpremium().equals("")&&!bb.getJqpremium().equals("0")&&!bb.getJqpremium().equals("0.00")){
        		bb.setJqpremium(decimalFormat.format(Float.parseFloat(bb.getJqpremium())));
        	}
        	insuranceDetails.setBaseinfor(bb);
        }
        if(insuranceDetails.getTaxinfor()!=null){
        	if(insuranceDetails.getTaxinfor().getSumuptax()!=null&&!insuranceDetails.getTaxinfor().getSumuptax().equals("")){
        		insuranceDetails.getTaxinfor().setSumuptax(decimalFormat.format(Float.parseFloat(insuranceDetails.getTaxinfor().getSumuptax())));
        	}
        }
        
		session.setAttribute(insuranceDetails.getBaseinfor().getOrderno()+"insuranceDetailsVO", insuranceDetails);
		 session.setAttribute("errorCode", insuranceDetails.getUserinfor().getErrorCode());
		 session.setAttribute("errorMessage", insuranceDetails.getUserinfor().getErrorMessage());
		return insuranceDetails;
	}


   
//	public String saveBaseDate(Baseinfor baseinfor,
//			HttpServletRequest httprequest) {
//		HttpSession session=httprequest.getSession();
//		InsuranceDetailsVO insuranceDetails=null;
//		if(session.getAttribute("insuranceDetailsVO")!=null){
//		 insuranceDetails = (InsuranceDetailsVO)session.getAttribute("insuranceDetailsVO");
//		}
//		if (insuranceDetails == null) {
//			InsuranceDetailsVO insuranceDetailsVo=ProcessData.initInsuranceDetailsVO();
//			insuranceDetails=insuranceDetailsVo;
//		}
//		insuranceDetails=ProcessData.setObjValuetoinsuranceDetails(baseinfor,insuranceDetails);
//		insuranceDetails=dataManageService.updateBaseData(insuranceDetails);
//		session.setAttribute("insuranceDetailsVO", insuranceDetails);
//		return "";
//	}



	public InsuranceDetailsVO savePremiumInfor(ModelMap modelMap,HttpServletRequest httprequest) {
		String orderNo = httprequest.getParameter("orderNo");
		HttpSession session = httprequest.getSession();
		InsuranceDetailsVO insuranceDetails=null;
		if(StringUtils.checkStringEmpty(orderNo)
				&& session.getAttribute(orderNo + "insuranceDetailsVO") != null){
		 insuranceDetails = (InsuranceDetailsVO)session.getAttribute(orderNo+"insuranceDetailsVO");
		}
		if (insuranceDetails == null) {
			InsuranceDetailsVO insuranceDetailsVo = ProcessData.initInsuranceDetailsVO();
			insuranceDetails = insuranceDetailsVo;
		}
		if(insuranceDetails.getUserinfor().getPageFlag()<4){//返回保费计算重新提交的数据
			insuranceDetails = premiumDataManageService.savePremiuminfor(insuranceDetails);
			insuranceDetails.getUserinfor().setPageFlag(4); //首次进入人员信息(personinfor)页面,将pageFlag状态改为4
		}else{
			insuranceDetails = premiumDataManageService.updatePremiuminfor(insuranceDetails);
		}
		for(Coverageinfor coverage:insuranceDetails.getCoverageinfors()){
			if("030107".equals(coverage.getInsrnccode())){
				if("0".equals(coverage.getSuminsured())){
					coverage.setSuminsured("");
				}
			}
			if("0.00".equals(coverage.getNyl12())){
				coverage.setNyl12("");
			}
		}
		session.setAttribute(insuranceDetails.getBaseinfor().getOrderno()+"insuranceDetailsVO", insuranceDetails);
		session.removeAttribute("appCoypCheckbox");
		session.removeAttribute("insureCopyCheckbox");
		session.removeAttribute("deliveryCopyCheckbox");
		return insuranceDetails;
	}
	
}
